package com.sai.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sai.common.api.vo.Result;
import com.sai.common.utils.ConvertUtils;
import com.sai.system.entity.SysDepartPermission;
import com.sai.system.entity.SysPermissionDataRule;
import com.sai.system.service.ISysDepartPermissionService;
import com.sai.system.service.ISysPermissionDataRuleService;
import com.sai.system.service.ISysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
* @Description: 部门权限表
*/
@Slf4j
@RestController
@RequestMapping("/sys/sysDepartPermission")
public class SysDepartPermissionController {
   @Autowired
   private ISysDepartPermissionService sysDepartPermissionService;

    @Autowired
    private ISysPermissionDataRuleService sysPermissionDataRuleService;

   /**
   * 部门管理授权查询数据规则数据
   */
   @GetMapping(value = "/datarule/{permissionId}/{departId}")
   public Result<?> loadDatarule(@PathVariable("permissionId") String permissionId, @PathVariable("departId") String departId) {
       List<SysPermissionDataRule> list = sysPermissionDataRuleService.getPermRuleListByPermId(permissionId);
       if(list==null || list.size()==0) {
           return Result.error("未找到权限配置信息");
       }else {
           Map<String,Object> map = new HashMap<>();
           map.put("datarule", list);
           LambdaQueryWrapper<SysDepartPermission> query = new LambdaQueryWrapper<SysDepartPermission>()
                .eq(SysDepartPermission::getPermissionId, permissionId)
                .eq(SysDepartPermission::getDepartId,departId);
           SysDepartPermission sysDepartPermission = sysDepartPermissionService.getOne(query);
           if(sysDepartPermission==null) {
            //return Result.error("未找到角色菜单配置信息");
           }else {
               String drChecked = sysDepartPermission.getDataRuleIds();
               if(ConvertUtils.isNotEmpty(drChecked)) {
                   map.put("drChecked", drChecked.endsWith(",")?drChecked.substring(0, drChecked.length()-1):drChecked);
               }
           }
           return Result.ok(map);
           //TODO 以后按钮权限的查询也走这个请求 无非在map中多加两个key
       }
   }

   /**
   * 保存数据规则至部门菜单关联表
   */
   @PostMapping(value = "/datarule")
   public Result<?> saveDatarule(@RequestBody JSONObject jsonObject) {
       try {
           String permissionId = jsonObject.getString("permissionId");
           String departId = jsonObject.getString("departId");
           String dataRuleIds = jsonObject.getString("dataRuleIds");
           log.info("保存数据规则>>"+"菜单ID:"+permissionId+"部门ID:"+ departId+"数据权限ID:"+dataRuleIds);
           LambdaQueryWrapper<SysDepartPermission> query = new LambdaQueryWrapper<SysDepartPermission>()
                .eq(SysDepartPermission::getPermissionId, permissionId)
                .eq(SysDepartPermission::getDepartId,departId);
           SysDepartPermission sysDepartPermission = sysDepartPermissionService.getOne(query);
           if(sysDepartPermission==null) {
               return Result.error("请先保存部门菜单权限!");
           }else {
               sysDepartPermission.setDataRuleIds(dataRuleIds);
                this.sysDepartPermissionService.updateById(sysDepartPermission);
           }
       } catch (Exception e) {
            log.error("SysDepartPermissionController.saveDatarule()发生异常：" + e.getMessage(),e);
            return Result.error("保存失败");
       }
       return Result.ok("保存成功!");
   }

}
